#include <REGX52.H>
#include "I2C.h"

// AT24C02的固定地址为1010，可配置地址本开发板上为000，最后一位写为0，读为1
#define AT24C02_ADDRESS 0xA0

/** @bref AT24C02写入一个字节
  * @param wordAddress 写入字节的地址
  * @param byte 写入的字节
  * @retval
  */
void AT24C02_writeByte(unsigned char wordAddress, byte)
{
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS);
	receiveACK();
	I2C_SendByte(wordAddress);
	receiveACK();
	I2C_SendByte(byte);
	receiveACK();
	I2C_Stop();
}

/** @bref AT24C02读取一个字节
  * @param 读取字节的地址
  * @retval 读取的字节
  */
unsigned char AT24C02_readByte(unsigned char wordAddress)
{
	unsigned char byte;
	
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS);
	receiveACK();
	I2C_SendByte(wordAddress);
	receiveACK();
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS | 0x01);
	receiveACK();
	byte = I2C_ReceiveByte();
	I2C_sendACK(1);
	I2C_Stop();
	
	return byte;
}
